home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Moscow ML 1.42 / src / mosmllib / Array.mlp < prev    next >
Encoding:
Text File  |  1997-08-18  |  5.6 KB  |  199 lines  |  [TEXT/R*ch]

  1. (* Array -- new basis 1994-11-21, 1995-05-21 *)
  2.  
  3. abstraction Array : Array = struct
  4.  
  5. (* In fact, type 'a array = 'a array_ ref, but for the static equality
  6.  * type to be right, we need to declare it a prim_EQtype:              *)
  7. prim_EQtype 'a array;
  8.  
  9. local 
  10.     prim_type 'a array_;
  11.  
  12.     type 'a vector = 'a Vector.vector;
  13.  
  14.     prim_val length_  : 'a array_ -> int               = 1 "vect_length";
  15.     prim_val lengthv_ : 'a vector -> int               = 1 "vect_length";
  16.  
  17.     prim_val array_  : int -> 'x -> 'a array_          = 2 "make_ref_vect";
  18.     (* array_ has a non-imperative type for the sake of array0, and a
  19.        very flexible type 'x to allow initialization.  Thus type
  20.        correctness inside this unit body depends on type annotations.
  21.     *)
  22.  
  23.     prim_val vector_ : int -> 'x -> 'a vector          = 2 "make_vect";
  24.     prim_val sub_    : 'a array_ -> int -> 'a          = 2 "get_vect_item";
  25.     prim_val subv_   : 'a vector -> int -> 'a          = 2 "get_vect_item";
  26.     prim_val update_ : 'a array_ -> int -> 'a -> unit  = 3 "set_vect_item";
  27.     prim_val updatev : 'a vector -> int -> 'a -> unit  = 3 "set_vect_item";
  28.  
  29.     prim_val magic   : 'a -> 'b                        = 1 "identity";
  30.  
  31.     fun from_array (a : 'a  array)  = !(magic a)    : 'a array_;
  32.     fun make_array (a : '_a array_) = magic (ref a) : 'a array
  33. in 
  34.  
  35. #include "../config/m.h"
  36. #ifdef SIXTYFOUR
  37. val maxLen = 18014398509481983; (* = 2^54-1, for 64-bit architectures *)
  38. #else
  39. val maxLen = 4194303;       (* = 2^22-1, for 32-bit architectures *)
  40. #endif
  41.  
  42. fun array(n, v : '_a) =
  43.   if n < 0 orelse n > maxLen then raise Size 
  44.   else make_array (array_ n v) : '_a array;
  45.  
  46. fun tabulate(n, f : int -> '_a) =
  47.   if n < 0 orelse n > maxLen then raise Size else
  48.   let val a = array_ n () : '_a array_
  49.       fun init i = if i >= n then () else (update_ a i (f i); init (i+1))
  50.   in (init 0; make_array a : '_a array) end;
  51.  
  52. fun fromList (vs : '_a list) =
  53.     let val n = List.length vs
  54.     val a = if n > maxLen then raise Size
  55.         else (array_ n () : '_a array_)
  56.     fun init [] i = ()
  57.       | init (v::vs) i = (update_ a i v; init vs (i+1))
  58.     in (init vs 0; make_array a : '_a array) end;
  59.  
  60. fun length a = length_ (from_array a);
  61.  
  62. fun sub(a, i) =
  63.     let val a = from_array a 
  64.     in
  65.     if i < 0 orelse i >= length_ a then raise Subscript 
  66.     else sub_ a i 
  67.     end
  68.  
  69. fun update(a, i, v) =
  70.     let val a = from_array a 
  71.     in
  72.     if i < 0 orelse i >= length_ a then raise Subscript 
  73.     else update_ a i v
  74.     end
  75.  
  76. fun extract (a : 'a array, i, slicelen) =
  77.     let val a = from_array a : 'a array_ 
  78.     val n = case slicelen of NONE => length_ a - i | SOME n => n
  79.     val newvec = if i<0 orelse n<0 orelse i+n > length_ a 
  80.              then raise Subscript
  81.              else vector_ n () : 'a vector
  82.     fun copy j = 
  83.         if j<n then
  84.         (updatev newvec j (sub_ a (i+j)); copy (j+1))
  85.         else
  86.         ()
  87.     in copy 0; newvec end;
  88.  
  89. fun copy {src=a1: 'a array, si=i1, len, dst=a2: 'a array, di=i2} =
  90.     let val a1 = from_array a1
  91.     and a2 = from_array a2
  92.     val n = case len of NONE => length_ a1 - i1 | SOME k => k
  93.     in
  94.     if n<0 orelse i1<0 orelse i1+n > length_ a1
  95.         orelse i2<0 orelse i2+n > length_ a2
  96.     then 
  97.         raise Subscript
  98.     else if i1 < i2 then        (* copy from high to low *)
  99.              let fun hi2lo j = 
  100.              if j >= 0 then
  101.              (update_ a2 (i2+j) (sub_ a1 (i1+j)); hi2lo (j-1))
  102.              else ()
  103.          in hi2lo (n-1) end
  104.          else                       (* i1 >= i2, copy from low to high *)
  105.          let fun lo2hi j = 
  106.              if j < n then
  107.              (update_ a2 (i2+j) (sub_ a1 (i1+j)); lo2hi (j+1))
  108.              else ()
  109.          in lo2hi 0 end
  110.     end
  111.  
  112. fun copyVec {src=a1: 'a vector, si=i1, len, dst=a2: 'a array, di=i2} =
  113.     let val a2 = from_array a2
  114.     val n = case len of NONE => lengthv_ a1 - i1 | SOME k => k
  115.     in
  116.     if n<0 orelse i1<0 orelse i1+n > lengthv_ a1
  117.            orelse i2<0 orelse i2+n > length_ a2
  118.         then 
  119.         raise Subscript
  120.     else 
  121.         let fun lo2hi j = if j < n then
  122.         (update_ a2 (i2+j) (subv_ a1 (i1+j)); lo2hi (j+1))
  123.                   else ()
  124.         in lo2hi 0 end
  125.     end;
  126.  
  127. fun foldl f e a = 
  128.     let val a = from_array a
  129.     val stop = length_ a
  130.     fun lr j res = if j < stop then lr (j+1) (f(sub_ a j, res))
  131.                else res
  132.     in lr 0 e end
  133.  
  134. fun foldr f e a =
  135.     let val a = from_array a
  136.     fun rl j res = if j >= 0 then rl (j-1) (f(sub_ a j, res))
  137.                else res
  138.     in rl (length_ a - 1) e end
  139.  
  140. fun modify f a = 
  141.     let val a = from_array a
  142.     val stop = length_ a
  143.     fun lr j = if j < stop then (update_ a j (f(sub_ a j)); lr (j+1))
  144.            else ()
  145.     in lr 0 end
  146.  
  147. fun app f a = 
  148.     let val a = from_array a
  149.     val stop = length_ a
  150.     fun lr j = if j < stop then (f(sub_ a j); lr (j+1))
  151.            else ()
  152.     in lr 0 end
  153.  
  154. fun sliceend (a, i, NONE) = 
  155.         if i<0 orelse i>length a then raise Subscript
  156.     else length a
  157.   | sliceend (a, i, SOME n) = 
  158.     if i<0 orelse n<0 orelse i+n>length a then raise Subscript
  159.     else i+n;
  160.  
  161. fun foldli f e (slice as (a, i, _)) = 
  162.     let val a = from_array a
  163.     fun loop stop =
  164.         let fun lr j res = 
  165.         if j < stop then lr (j+1) (f(j, sub_ a j, res))
  166.         else res
  167.         in lr i e end
  168.     in loop (sliceend slice) end;
  169.  
  170. fun foldri f e (slice as (a, i, _)) = 
  171.     let val a = from_array a
  172.     fun loop start =
  173.         let fun rl j res = 
  174.             if j >= i then rl (j-1) (f(j, sub_ a j, res))
  175.             else res
  176.         in rl start e end;
  177.     in loop (sliceend slice - 1) end
  178.  
  179. fun modifyi f (slice as (a, i, _)) = 
  180.     let val a = from_array a
  181.     fun loop stop =
  182.         let fun lr j = 
  183.         if j < stop then (update_ a j (f(j, sub_ a j)); lr (j+1))
  184.         else ()
  185.         in lr i end
  186.     in loop (sliceend slice) end;
  187.  
  188. fun appi f (slice as (a, i, _)) = 
  189.     let val a = from_array a
  190.     fun loop stop = 
  191.         let    fun lr j = 
  192.             if j < stop then (f(j, sub_ a j); lr (j+1)) 
  193.             else ()
  194.         in lr i end
  195.     in loop (sliceend slice) end;
  196. end
  197.  
  198. end
  199.